package com.group2.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.group2.bean.KeywordBean;
import com.group2.bean.StayTimeBySourceBean;
import com.group2.service.KeywordStaticService;
import com.group2.service.StayTimeBySourceService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 高耀
 * @date 2024/12/17 16:04<p></p>
 */
@RestController
@RequestMapping("/gaoyao")
public class AllStaticsController {
    @Autowired
    KeywordStaticService staticService;
    @Autowired
    StayTimeBySourceService stayTimeBySourceService;
    @RequestMapping("/kwstatic")
    public String getKeyWordStatic(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        //判断是否传参
        if(date == 0) {
            //给一个当前日期的值
            date = Integer.valueOf(DateFormatUtils.format(System.currentTimeMillis(), "yyyyMMdd"));
        }
        //调用service层方法查询得到一个List
        List<KeywordBean> beanList = staticService.getKeywordStatic(date);

        /*
               返回格式
            {
              "status": 0,
              "data": [
                {
                  "name": "visualMap",
                  "value": 22199,
                  "url":"...."
                },
                {
                  "name": "data",
                  "value": 60679,
                  "url":"..."
                }
              ]
            }
         */
        //创建data对象数组
        JSONArray dataJSONObj = new JSONArray();
        //遍历查询结果放入
        for (KeywordBean bean : beanList) {
            //创建一个JSON对象
            JSONObject tempObj = new JSONObject();
            tempObj.put("name", bean.getName());
            tempObj.put("value", bean.getValue());
            tempObj.put("url", "cn.bing.com/search?q=" + bean.getName() + "&pq=" + bean.getName());
            //放入data数组
            dataJSONObj.add(tempObj);
        }

        //创建外层对象
        JSONObject outerJsonObj = new JSONObject();
        outerJsonObj.put("status", 0);
        outerJsonObj.put("data", dataJSONObj);
        //最后转成String形式返回
        return outerJsonObj.toJSONString();
    }


    @RequestMapping("/staytimebysc")
    public String getStayTimeBySource(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        //判断是否传参
        if(date == 0) {
            //给一个当前日期的值
            date = Integer.valueOf(DateFormatUtils.format(System.currentTimeMillis(), "yyyyMMdd"));
        }
        //调用Service层方法获取List
        List<StayTimeBySourceBean> beanList = stayTimeBySourceService.getStayTimeBySource(date);
        /*
            数据格式：
                {
                  "status": 0,
                  "data": {
                    "categories": [
                      "baidu",
                      "360",
                      "toutiao",
                      "tiktok"
                    ],
                    "series": [
                      {
                        "name": "会话平均停留时长",
                        "data": [
                          9580,
                          9797,
                          6027,
                          6659
                        ]
                      }
                    ]
                  }
                }

            使用拼接的方法
         */
        StringBuilder sb = new StringBuilder();
        sb.append("{\"status\": 0,\"data\": {\"categories\":[");
        //来源列表
        ArrayList<String> valueList = new ArrayList<>();
        //平均访问时间列表
        ArrayList<Double> avgTimeList = new ArrayList<>();
        beanList.forEach(
                bean -> avgTimeList.add(bean.getDuringTime())
        );

        //遍历拼接名字
        for (int i = 0; i < beanList.size(); i++) {
            sb.append("\"").append(beanList.get(i).getSourceName());
            if (i == beanList.size() - 1) {
                sb.append("\"");
            } else {
                sb.append("\",");
            }
        }

        sb.append("],\"series\": [{\"name\": \"会话平均停留时长\",\"data\":")
                .append(avgTimeList)
                .append("}]}}");
        //返回拼接结果
        return sb.toString();
    }
}
